package org.jeecg.modules.flowerutil;
/**
 * @author gf
 * @create 2022-9-15 15:48
 */

import org.jeecg.common.system.vo.DictModel;
import org.jeecg.modules.DateUtils.GetDate;
import org.jeecg.modules.flower.entity.FlowerAccountPeriodSwitch;
import org.jeecg.modules.flower.service.IFlowerAccountPeriodSwitchService;
import org.jeecg.modules.flower.util.CommonUtil;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 采后处理--花----账期处理逻辑
 *
 * @author gf
 * @ClassName PeriodUtils
 * @description: TODO
 * @datetime 2022年 09月 15日 15:48
 * @version: 1.0
 **/
@Component
public  class PeriodUtils {
    @Autowired
    private IFlowerAccountPeriodSwitchService flowerAccountPeriodSwitchService;

    private static IFlowerAccountPeriodSwitchService flowerAccountPeriodSwitchService2;

    @PostConstruct
    public void init2(){
        PeriodUtils.flowerAccountPeriodSwitchService2 = flowerAccountPeriodSwitchService ;
    }

    /**
    *获取默认的账期
    * */
    public static String getDefaultPeriod(){
        List<FlowerAccountPeriodSwitch> listSwitch=flowerAccountPeriodSwitchService2.list();
        String settleTime=listSwitch.get(0).getSettleTime();//结账日期

        String currentDay = GetDate.getCurrentDay();
        String result="";
        // 如果当前的日小于等于结账日期，那么默认账期就是当前月的账期，否则默认账期就是下个月的账期
        if (Integer.parseInt(currentDay)<=Integer.parseInt(settleTime)){
            result=GetDate.getCurrentYearAndMonth();
        }else {
            result=GetDate.getNextYearAndMonth();
        }
        return result;
    }

    public List<String> getPeriod(){
        List<FlowerAccountPeriodSwitch> listSwitch=flowerAccountPeriodSwitchService2.list();
        int state=listSwitch.get(0).getState();//账期开关（0：关闭，1：打开）
        String period = listSwitch.get(0).getPeriod();// 账期

        List<String> resultList=new ArrayList<>();
        if (state==0){// 如果账期已关闭，那么只返回默认账期
            resultList.add(this.getDefaultPeriod());
        } else {
            resultList.add(period);//添加账期开关中设置的账期进resultList
            String defaultPeriod=this.getDefaultPeriod();
            resultList.add(defaultPeriod);//添加默认账期进resultList

            String currentYearAndMonth = GetDate.getCurrentYearAndMonth();
            //如果默认账期大于当前年月则添加下个月的下个月账期进resultList，否则添加下个月的账期进resultList
            if (Integer.parseInt(defaultPeriod)>Integer.parseInt(currentYearAndMonth)){
                resultList.add(GetDate.getNextNextYearAndMonth());
            }else if(Integer.parseInt(defaultPeriod)==Integer.parseInt(currentYearAndMonth)){
                resultList.add(GetDate.getNextYearAndMonth());
            }
        }
        List<String> newResultList=resultList.stream().distinct().collect(Collectors.toList());// list去重
        return newResultList;
    }


    //获取当前账期
    //是否提示选择账期，账期选择

    @Autowired
    private ISysDictService sysDictService;

    private static ISysDictService sysDictService2;

    @PostConstruct
    public void init(){
        PeriodUtils.sysDictService2 = sysDictService ;
    }

    public String getNowPeriod() {
        List<DictModel> chPeriodList = PeriodUtils.sysDictService2.getDictItems("ch_period");
        Integer endDay = 25; //账期结束日
        Integer showDay = 5; //提醒天数
        if (chPeriodList.size() == 0) {
            throw new RuntimeException("请联系管理员配置数据字典：采后账期（ch_period）1：账期结束日；2：提醒天数");
        }
        for (DictModel period : chPeriodList) {
            String value = period.getValue();
            String text = period.getText();
            if ("1".equals(value)) {
                endDay = Integer.parseInt(text);
            } else if ("2".equals(value)) {
                showDay = Integer.parseInt(text);
            }
        }
        Calendar calendar = Calendar.getInstance();
//        SimpleDateFormat formatter = new SimpleDateFormat( "dd-MM-yyyy HH:mm:ss");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM");
        String periodNow = formatter.format(calendar.getTime());
        Integer nowYear = calendar.get(Calendar.YEAR);
        Integer nowDate = calendar.get(Calendar.DAY_OF_MONTH);
        Integer nowMonth = calendar.get(Calendar.MONTH)+1;
        if (nowDate > endDay) {
            periodNow = nowYear + "" + CommonUtil.subLength((nowMonth + 1),2);
        } else {
            periodNow = nowYear + "" + CommonUtil.subLength(nowMonth,2);
        }
        return periodNow;
    }


    /**
     * 返回上一个账期
     *
     * @return java.lang.String
     * @descriptionauthor
     * @author gf
     * 2022-9-16 9:25
     * []
     */
    public String getPrePeriod() {
        List<DictModel> chPeriodList = PeriodUtils.sysDictService2.getDictItems("ch_period");
        Integer endDay = 25; //账期结束日
        Integer showDay = 5; //提醒天数
        if (chPeriodList.size() == 0) {
            throw new RuntimeException("请联系管理员配置数据字典：采后账期（ch_period）1：账期结束日；2：提醒天数");
        }
        for (DictModel period : chPeriodList) {
            String value = period.getValue();
            String text = period.getText();
            if ("1".equals(value)) {
                endDay = Integer.parseInt(text);
            } else if ("2".equals(value)) {
                showDay = Integer.parseInt(text);
            }
        }
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM");
        String periodNow = formatter.format(calendar.getTime());
        int nowYear = calendar.get(Calendar.YEAR);
        int nowDate = calendar.get(Calendar.DAY_OF_MONTH);
        int nowMonth = calendar.get(Calendar.MONTH)+1;
        if (nowDate > endDay) {
            periodNow = nowYear + "" + CommonUtil.subLength((nowMonth),2);
        } else {
            periodNow = nowYear + "" + CommonUtil.subLength((nowMonth - 1),2);
        }
        return periodNow;
    }

    /**
     * 返回是否还行提醒
     *
     * @return java.lang.Integer
     * @descriptionauthor
     * @author gf
     * 2022-9-16 9:27
     * []
     */
    public boolean getShowDay() throws ParseException {
        List<DictModel> chPeriodList = PeriodUtils.sysDictService2.getDictItems("ch_period");
        Integer endDay = 25; //账期结束日
        Integer showDay = 5; //提醒天数
        if (chPeriodList.size() == 0) {
            throw new RuntimeException("请联系管理员配置数据字典：采后账期（ch_period）1：账期结束日；2：提醒天数");
        }
        for (DictModel period : chPeriodList) {
            String value = period.getValue();
            String text = period.getText();
            if ("1".equals(value)) {
                endDay = Integer.parseInt(text);
            } else if ("2".equals(value)) {
                showDay = Integer.parseInt(text);
            }
        }
        Calendar calendar = Calendar.getInstance();
        int nowYear = calendar.get(Calendar.YEAR);
        int nowDate = calendar.get(Calendar.DAY_OF_MONTH);
        int nowMonth = calendar.get(Calendar.MONTH)+1;

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String now = simpleDateFormat.format(new Date());
        String endDate = "";
        if (nowDate > endDay) {
            //当前日期大于25号，则
            endDate = nowYear + "-" + nowMonth + "-" + endDay;
        } else {
            endDate = nowYear + "-" + (nowMonth - 1) + "-" + endDay;
        }
        calendar.setTime(simpleDateFormat.parse(now));
        long timeInMillis = calendar.getTimeInMillis();

        calendar.setTime(simpleDateFormat.parse(endDate));
        long timeInMillis2 = calendar.getTimeInMillis();
        long l = timeInMillis - timeInMillis2;
        long l1 = l / (1000 * 3600 * 24);

        boolean rest = false;
        if(l1<showDay){
            rest =true ;
        }else {
            rest =false ;
        }
        return rest;
    }

}